<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->

        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd(' ')">添加一级部门</el-dropdown-item>

                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="data" :props="defaultProps" :default-expand-all="true">
          <template v-slot="{ node, data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ node.label }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item :close-on-press-escape="false" @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>

                        <el-dropdown-item @click.native="edit(data.id)">编辑部门</el-dropdown-item>

                        <el-dropdown-item @click.native="dels(data.id)">删除部门</el-dropdown-item>

                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree></el-card>
      <el-dialog :title="isEmit?'编辑部门':'添加部门'" :visible.sync="showDialog" :close-on-click-modal="false" :close-on-press-escape="false">
        <Dalog v-if="showDialog" ref="dalogref" :xiaoyanlist="list" :pid="chuanzhi" :is-emit="isEmit" @success="successs" @quxiao="quxiao" /></el-dialog>
    </div>
  </div>
</template>
<script>
import { getmenus, dodel } from '@/api/departments'
import Dalog from './dalog.vue'
export default {
  components: { Dalog },
  data() {
    return {
      showDialog: false,
      data: [],
      isEmit: false,

      defaultProps: {
        children: 'children',
        label: 'name'
      },
      chuanzhi: '',
      list: []
    }
  },
  created() {
    this.getmenus()
    console.log(this.fn1())
  },
  methods: {
    hAdd(id) {
      this.showDialog = true
      this.chuanzhi = id
      this.isEmit = false
    },
    edit(id) {
      this.chuanzhi = id
      this.showDialog = true
      this.isEmit = true
      this.$nextTick(() => {
        this.$refs.dalogref.emitbumens()
      })
    },
    async dels(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        dodel(id)
        this.getmenus()

        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async getmenus() {
      const { data: res } = await getmenus()
      console.log(res)
      res.depts.shift()
      console.log(res.depts)
      this.list = res.depts.map(item => { return { id: item.id, name: item.name, code: item.code, pid: item.pid } })
      const aaa = res.depts.filter((item) => {
        item.children = res.depts.filter((ele) => {
          return item.id === ele.pid
        })
        return res.depts
      })
      this.data = aaa
      // console.log(aaa)
    },
    successs() {
      this.showDialog = false
      this.getmenus()
      this.$message.success('添加成功')
    },
    quxiao() {
      this.showDialog = false
    },
    fn1() {
      const list = [
        { 'id': '29', 'pid': '', 'name': '总裁办' },
        { 'id': '2c', 'pid': '', 'name': '财务部' },
        { 'id': '2d', 'pid': '2c', 'name': '财务核算部' },
        { 'id': '2f', 'pid': '2c', 'name': '薪资管理部' },
        { 'id': 'd2', 'pid': '', 'name': '技术部' },
        { 'id': 'd3', 'pid': 'd2', 'name': 'Java研发部' }
      ]
      const treeList = [] // 最终要产出的树状数据的数组
      list.filter(item => {
        item.children = list.filter(ele => { return item.id === ele.pid })
        return !item.pid
      })
      // console.log(arr)
      const map = {}
      list.forEach(item => {
        map[item.id] = item
      })
      list.forEach(ele => {
        // console.log(map[ele.pid])
        if (map[ele.pid]) {
          map[ele.pid].children = ele
        } else {
          treeList.push(ele)
        }
      })
      return treeList
    }
  }
}
</script>
